package helper;

import sort.insertionSort.InsertionSort;
import sort.mergeSort.MergeSort;
import sort.selectionSort.SelectionSort;

public class SortingHelper {

    private SortingHelper() {
    }

    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if (arr[i - 1].compareTo(arr[i]) > 0) {
                return false;
            }
        }
        return true;
    }

    public static <E extends Comparable<E>> void sortTest(String sortName, E[] arr) {
        long startTime = System.nanoTime();
        if (sortName.equals("SelectionSort")) {
            SelectionSort.sort(arr);
        } else if (sortName.equals("InsertSort")) {
            InsertionSort.sort(arr);
        } else if (sortName.equals("MergeSort")) {
            MergeSort.sort(arr);
        }
        long endTime = System.nanoTime();
        double time = (endTime - startTime) / 1000000000.0;
        if (!isSorted(arr)) {
            throw new RuntimeException("Sort Failed");
        }
        System.out.println(sortName + ",time:" + time + "s");
    }

}
